package mwt.xml.waxdbforms.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import mwt.xml.waxdbforms.business.WAXDBFormsServiceFactory;
import mwt.xml.waxdbforms.business.impl.WAXDBFormsServiceFactoryImpl;
import mwt.xml.xdbforms.dbms.XDBFManager;
import mwt.xml.xdbforms.dbms.impl.MySqlXDBFManager;

public class WAXDBFContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        InputStream is = null;
        try {
            ServletContext servletContext = sce.getServletContext();
            String resource = servletContext.getRealPath("/WEB-INF/waxdbforms.properties");
            is = new FileInputStream(resource);

            XDBFManager.setInstance(new MySqlXDBFManager(is));
            XDBFManager xdbfm = XDBFManager.getManager();

            WAXDBFormsServiceFactory.setInstance(new WAXDBFormsServiceFactoryImpl(xdbfm));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WAXDBFContextListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WAXDBFContextListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(WAXDBFContextListener.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(WAXDBFContextListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void contextDestroyed(ServletContextEvent sce) {

    }
}
